﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace SPMigrationOracleUtility
{
    public partial class RunMapping : Form
    {
        Timer timer1 = new Timer();
        RunMappingComp runMapComp = new RunMappingComp();
        Map_Migration obj = new Map_Migration();
        bool status = false;
        int cnt;
        public string migModes { get; set; }
    
        /// <summary>
        /// Empty Constructor of RunMapping Class
        /// </summary>
        /// 

        public RunMapping(string migMode)
        {
            InitializeComponent();
            chkLstBox.Items.Clear();

            runMapComp.RetrieveMappingDetails(chkLstBox, migMode);
            
            ColumnHeader ch1 = lstView.Columns.Add("Mapping Details", 740);
            lstView.Items.Add("");
            migModes = migMode;
        }

        /// <summary>
        /// Button Click Event, passing the mapping details to RetrieveMappingItemDetails of RumMappingComp Class
        /// </summary>
        /// <param name="sender">Event parameter</param>
        /// <param name="e">Event parameter</param>
        private void btnMigRun_Click(object sender, EventArgs e)
        {
            try
            {
                Stopwatch objWatch = new Stopwatch();
                //lstView.EnsureVisible(lstView.Items.Count - 1);
                
                if (chkLstBox.CheckedItems.Count > 0)
                {
                    lstView.Items.Clear();
                    progressBar1.Maximum = chkLstBox.CheckedItems.Count * 5;
                    progressBar1.Show();
                    progressBar1.Increment(1);
                    progressBar1.PerformStep();
                    foreach (MappingListValues lstitm in chkLstBox.CheckedItems)
                    {
                        progressBar1.PerformStep();
                        objWatch.Start();
                        status= runMapComp.RetrieveMappingItemDetails(lstitm,progressBar1,lstView , imgLst,lblTimer,objWatch);
                        lblTimer.Text = Convert.ToString(objWatch.Elapsed);
                        progressBar1.Value = 0;
                    }

                    if (status)
                    {
                        MessageBox.Show("Migration Completed Sucessfully");
                    }
                
                    progressBar1.Hide();
                    progressBar1.Value = 0;
                    objWatch.Stop();
                }

                else
                {
                    MessageBox.Show("Please Select the Mapping Name");
                }
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }

            #region Old
            //listView1.Items.Clear();
            //if (chkLstBox.CheckedItems.Count > 0)
            //{
            //progressBar1.Maximum = chkLstBox.CheckedItems.Count * 5;
            //progressBar1.Show();
            //progressBar1.Increment(10);
            //progressBar1.PerformStep();
            //    foreach (MappingListValues lstitm in chkLstBox.CheckedItems)
            //    {
            //        timer1.Enabled = true;
            //progressBar1.PerformStep();
            //        obj.importList(lstitm, progressBar1, listView1);
            //        progressBar1.PerformStep();
            //    }

            //foreach (ListViewItem itemRow in this.listView1.Items)
            //{
            //    textBox1.Text = textBox1.Text + System.Environment.NewLine + itemRow.SubItems[1].Text;
            //}


            //progressBar1.Hide();
            //progressBar1.Value = 0;
            //}
            
            //else
            //{
            //    MessageBox.Show("Please Select the Mapping Name");
            //}
            #endregion
        }

        /// <summary>
        /// Button Click Event- Close the form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMigcancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void RunMapping_Load(object sender, EventArgs e)
        {
            lblMapCnt.Text = "";
            if (migModes == "Excel Migration")
            {
                lblDisp.Text = "Excel Run Mapping";
                lblDisp.BackColor = Color.Green;
            }
            else
            {
                lblDisp.Text = "Oracle Run Mapping";
                lblDisp.BackColor = Color.Red;
            }

        }

        private void chkLstBox_SelectedValueChanged(object sender, EventArgs e)
        {
            cnt = 0;
            if (chkLstBox.CheckedItems.Count == 0)
            {
                lblMapCnt.Text = "";
            }
            else
            {
                foreach (MappingListValues lstitm in chkLstBox.CheckedItems)
                {
                    cnt = cnt + runMapComp.GetMapRecordCnt(lstitm);
                    lblMapCnt.Text = Convert.ToString(cnt);
                }
            }
        }
    }
}
